/* 
 * @(#)OnLineListener.java    Created on Apr 20, 2013
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.listener;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class OnLineListener implements ServletContextListener, HttpSessionAttributeListener {

    ServletContext application;

    public void contextDestroyed(ServletContextEvent sce) {

    }

    public void contextInitialized(ServletContextEvent sce) {
        application = sce.getServletContext();
        application.setAttribute("userList", new ArrayList());
    }

    public void attributeAdded(HttpSessionBindingEvent se) {
        ArrayList list = (ArrayList) application.getAttribute("userList");
        list.add(se.getValue());
        application.setAttribute("userList", list);
        System.out.println("add...");
    }

    public void attributeRemoved(HttpSessionBindingEvent se) {
        ArrayList list = (ArrayList) application.getAttribute("userList");
        list.remove(se.getValue());
        application.setAttribute("userList", list);
        System.out.println("remove...");
    }

    public void attributeReplaced(HttpSessionBindingEvent se) {
        ArrayList list = (ArrayList) application.getAttribute("userList");
        list.remove(se.getValue());
        list.add(se.getSession().getAttribute("user"));
        application.setAttribute("userList", list);
    }

}
